package com.verisette.ncos.web.accesscontrol.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

import org.corp.agi.service.bean.ServiceRequest;
import org.corp.agi.service.bean.ServiceResponse;
import org.corp.agi.service.bean.UserBean;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.constant.WebConstant;
import org.corp.agi.web.util.ServiceRequestFactory;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.service.accesscontrol.gateway.CustomAccessControlService;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CustomLoginAction extends BaseAction {
	private static final long serialVersionUID = -6607640980207386732L;

	private Logger logger = Logger.getLogger(CustomLoginAction.class);
	
	private  CustomAccessControlService customAccessControlService;
	@Autowired
	@Required
	public void setCustomAccessControlService(CustomAccessControlService customAccessControlService) {
		this.customAccessControlService = customAccessControlService;
	}
	
	protected String username;
	
	protected String password;
	
	private void clearSessionMessage(){
		httpServletRequest.getSession().removeAttribute("SESSION_MESSAGE_KEY");
	}
	
	public String init() throws Exception {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );

		if(httpServletRequest.getSession().getAttribute(WebConstant.SESSION_USERBEAN_KEY) == null)
		{
			clearSessionMessage();
	        return "login";
		}
		else{
			return ActionSupport.SUCCESS;	
		}
	}
	
	public String login() throws Exception {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": login" );
		clearSessionMessage();
		
		if(httpServletRequest.getSession().getAttribute(WebConstant.SESSION_USERBEAN_KEY) == null){
			
			//create userBean and save to session
			MasterUserProfile masUser = new MasterUserProfile();
			ServiceRequest<Map<String, Object>> request = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
			
			masUser.setUsername(this.username);
			masUser.setPassword(this.password);
			
			//Add by thippawan @2010-10-04

			Map<String, Object> loginInputMap = new HashMap<String, Object>();
			
			loginInputMap.put(ProjectConstant.USER_PROFILE, masUser);
			loginInputMap.put(ProjectConstant.USER_PROFILE_IP_ADDRESS, httpServletRequest.getRemoteAddr());
			
			request.setValueObject(loginInputMap);
			
			ServiceResponse<UserBean> response = customAccessControlService.customLogin(request);
			
			if(isServiceResponseSuccess(response))
			{
				UserBean loginUser = response.getValueObject();	
				httpServletRequest.getSession().setAttribute(WebConstant.SESSION_USERBEAN_KEY, response.getValueObject());
				
				// Check force to change password flag
				if(null!=response.getMessages()){
					String msgCode = response.getMessages().get(0).getMessageCode();
					
					if(msgCode.equals(ProjectConstant.CHANGE_PASSWORD_FORCE_TO_CHANE_PWD_FLAG)){
						return "changePassword";
					}
				}

			}
			else
			{
				return "login";
			}
			
		}
		return ActionSupport.SUCCESS;
	}
	
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
